<?php
class lib_game_quan extends lib_base{
	public $table_name = "qc_game_quan";
    public $pk = "id";

    public function get_datalist($cond,$sort='is_top desc,up_time desc,id desc'){

        // $this->confObj = D('game_jl_conf');

        // $this->tings = $this->confObj->get_conf('厅室');
        // $this->chaox = $this->confObj->get_conf('朝向');
        // $this->fangl = $this->confObj->get_conf('房龄');
        // $this->louc = $this->confObj->get_conf('楼层');
        // $this->leix = $this->confObj->get_conf('类型');
        // $this->chanq = $this->confObj->get_conf('产权');
        // $this->zhuangx = $this->confObj->get_conf('装修');
        // $this->mianjis = $this->confObj->get_conf('面积');
        // $this->jiages = $this->confObj->get_conf('租金');

        $result = $this->get_loaddata($cond,$sort,'*',5);
        
        if($result){
            $this->games = D('game_list')->ids($result['list'],'game_id','id,name');
            $this->cates = D('game_quan_cate')->ids($result['list'],'cate_id','id,name');
            $this->users = D('sys_user')->ids($result['list'],'user_id','id,nickName,avatarUrl,realname,tel,status');
            // $this->companys = D('company')->ids($result['list'],'company_id');
            // $this->stores = D('company_store')->ids($result['list'],'store_id','id,name');
            // $this->admins = D('company_user')->ids($result['list'],'admin_id');
            
            foreach ($result['list'] as $val) {

                if($val['photo']){
                    $photos_arr = explode(',', $val['photo']);
                    foreach($photos_arr as $v){
                        // $val['photos'][] = $v;
                        $v2 = D('upload_file')->get_image_url($v);
                        $val['photos'][] = $v2;
                        $val['thumbs'][] = $v2."_thumb.jpg";
                    }
                    $val['img'] = $val['photos'][0];
                }
                //标签
                // if($val['tags']){
                //     $val['tagss'] = D('house_tag')->findByIds($val['tags']);
                // }
                $val['game'] = $this->games[$val['game_id']];
                $val['cate'] = $this->cates[$val['cate_id']];
                $val['user'] = $this->users[$val['user_id']];

                // if($val['admin_id']){
                //     $user = D('company_user')->get_wxuser($val['admin_id']);
                //     if($user){
                //         $val['user_nickname'] = $user['nickName'];
                //         $val['user_face'] = $user['avatarUrl'];
                //         $val['user_realname'] = $user['realname'];
                //         $val['user_tel'] = $user['tel'];
                //         $val['user_id'] = $users['id'];
                //         $val['user_status'] = $users['status'];
                //     }
                    
                // }

                if($val['intro']){
                    // $val['intro'] = __htmlToArray($val['intro']);
                }
                
                // $val['house_jiage'] = $val['house_jiage'].' 万元';
                // $val['tings'] = $this->tings[$val['house_tings']];
                // $val['zhuangx'] = $this->zhuangx[$val['house_zhuangx']];
                $val['addtime'] = time_tran($val['add_time']);
                if($val['up_time']){
                    $val['addtime'] = time_tran($val['up_time']);
                }
                if($val['is_top']){
                    // $val['goods'] = '11111111';
                }

                //标签
                if($val['tags']){
                    // $val['tagss'] = D('game_jl_tag')->findByIds($val['tags']);
                }

                $val['sharetitle'] = $val['game']['name'].' '.$val['game']['intro'];

                //点赞
                $val['logusers'] = D('game_quan_log')->get_logs($val['id']);
                // //评论
                $val['replays'] = D('game_quan_log')->get_replays($val['id']);
                // //统计
                // $val['count'] = D('game_quan_log')->count_logs($val['id']);
                
                
                $list[] = $val;
            }
            //dump($list);exit;
            
            $data['list'] = $list;
            $data['page'] = $result['showPage']['total_page'];
        }else{
            return false;
        }
        return $data;
    }

    // ajax
    public function get_child($data){

        $this->obj = D("company_{$data['type']}");
        $type_ids = $data[$data['type'].'_ids'];

        $this->info = $this->obj->get_info($data['id']);
        $cond = "status=1 and parent_id={$data['id']}";
        if($type_ids){
            $cond .= " and id IN ({$type_ids})";
        }
        
        $list = $this->obj->findAll(array($cond),'sort asc,id asc');
        return $list;

    }

    public function get_detail($cond){
    	$this->order_logObj = D('company_order_log');

    	$info = $this->get_info($cond);

    	if($info){
	        if($info['photo']){
				$info['photos'] = explode(',', $info['photo']);
			}

			$cond = "status=1 and order_id={$info['id']}";
			$logs = $this->order_logObj->get_list($cond);
			if($logs['list']){
				foreach ($logs['list'] as $key => $val) {
					if($val['intro']){
						$val['intro'] = unserialize($val['intro']);
						if($val['intro']['photo']){
							$val['intro']['photos'] = explode(',', $val['intro']['photo']);
						}
						
					}
					$list[] = $val;
				}
				
				$info['logs'] = $list;
				$info['logs_maxid'] = max(array_keys($list));
			}
			
    	}
    	
    	return $info;

    }


    public function order_act($order_id,$data){
        $msg = array(
            '1'=>'提交了订单',
            '2'=>'重派了订单',
            '3'=>'确认了订单',
            '4'=>'订单维修中',
            '5'=>'完成了订单',
            '6'=>'评价了订单',
            '7'=>'结算了订单',
            '99'=>'取消了订单',
        );
        $intro['msg'] = $msg[$data['order_status']];
        if($data['order_status'] > 1){
            if($data['photo']) $intro['photo'] = joinStr($data['photo']);
            if($data['intro']) $intro['intro'] = $data['intro'];
        }

        $order = D('company_order')->get_info($order_id);
        if($order){

            $arrBase['shifu_id'] = $data['shifu_id'];
            $arrBase['order_status'] = $data['order_status'];
            $arrBase['status'] = 1;
            $arrBase['up_time'] = date('Y-m-d H:i:s');

            if($data['order_status']==5){
                $arrBase['money_wx'] = $data['money_wx'];
                $arrBase['money_rg'] = $data['money_rg'];
                $arrBase['money_info'] = $data['money_info'];
            }
            if($data['order_status']==6){
                $arrBase['star_wx'] = $data['star_wx'];
                $arrBase['star_sf'] = $data['star_sf'];
                $arrBase['star_info'] = $data['star_info'];
            }
            
            return $this->edit($order_id,$arrBase);
        }
        
    }

    public function pingjia($order_id,$data){
        $data['msg'] = '评价了订单';
        if($data['intro']) $data['intro'] = $data['intro'];

        $order = D('company_order')->get_info($order_id);
        if($order){
            $arrBase = array(
                'star_wx' =>$data['star_wx'],
                'star_info' =>$data['star_info'],
                'shifu_id' =>$data['shifu_id'],
                'order_status' =>$data['order_status'],
                'status' =>1,
                'up_time'=>date('Y-m-d H:i:s'),
            );
            return $this->edit($order_id,$arrBase);
        }
        
    }

    /**
     * 订单导出
     * @param $dataType
     * @param $query
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportList($cond,$data)
    {
        $query = $this->setWhere($cond,$data);

        // 获取订单列表
        $list = $this->findAll(array($query));
        if($list){
            $this->areas = D('company_area')->ids($list,'area_id');
            $this->cates = D('company_cate')->ids($list,'cate_id');
            $this->zones = D('company_zone')->ids($list,'zone_id');
            $this->admins = D('company_user')->ids($list,'admin_id');
            $this->shifus = D('company_user')->ids($list,'shifu_id');
            $this->devices = D('company_device')->ids($list,'device_id');

            $this->orderStatus = array('1'=>'新报修','2'=>'已派单','3'=>'已接单','4'=>'维修中','5'=>'已完成','6'=>'已评价','99'=>'已取消');
        

            // 表格标题
            $tileArray = ['订单号', '区域', '分类', '地点', '位置', '标题', '设备', '报修员',
                '师傅', '报修时间', '完成时间', '订单状态', '备注', '评价星际', '评价内容'];
            // 表格内容
            $dataArray = [];
            foreach ($list as $order) {
                $dataArray[] = [
                    '订单号' => $this->filterValue($order['id']),
                    '区域' => $this->areas[$order['area_id']]['name'],
                    '分类' => $this->cates[$order['cate_id']]['name'],
                    '地点' => $this->zones[$order['zone_id']]['name'],
                    '位置' => $this->filterValue($order['local']),
                    '标题' => $this->filterValue($order['title']),
                    '设备' => $this->devices[$order['device_id']]['name'],
                    '报修员' => $this->admins[$order['admin_id']]['realname'],
                    '师傅' => $this->shifus[$order['shifu_id']]['realname'],
                    '报修时间' => $order['add_time'],
                    '完成时间' => $order['up_time'],
                    '订单状态' => $this->orderStatus[$order['order_status']],
                    '备注' => $this->filterValue($order['intro']),
                    '评价星际' => $this->filterValue($order['star_wx']),
                    '评价内容' => $this->filterValue($order['star_info']),
                    // '是否已评价' => $this->filterValue($order['is_comment'] ? '是' : '否'),
                ];
            }
            // 导出csv文件
            $filename = 'order-' . date('YmdHis');
            export_excel($filename . '.csv', $tileArray, $dataArray);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    private function setWhere($cond,$query)
    {
        $cond = $cond;
        if (isset($query['keyword']) && !empty($query['keyword'])) {
            $cond.=" and (title like '%".$query['keyword']."%') ";
        }
        if (isset($query['area_id']) && !empty($query['area_id'])) {
            $cond .=" and area_id='{$query['area_id']}'";
        }
        if (isset($query['cate_id']) && !empty($query['cate_id'])) {
            $cond .=" and cate_id='{$query['cate_id']}'";
        }
        if (isset($query['zone_id']) && !empty($query['zone_id'])) {
            $cond .=" and zone_id='{$query['zone_id']}'";
        }
        if (isset($query['device_id']) && !empty($query['device_id'])) {
            $cond .=" and device_id='{$query['device_id']}'";
        }
        if (isset($query['shifu_id']) && !empty($query['shifu_id'])) {
            $cond .=" and shifu_id='{$query['shifu_id']}'";
        }
        if (isset($query['time_s']) && !empty($query['time_s'])) {
            $cond .=" and add_time>='{$query['time_s']}'";
        }
        if (isset($query['time_e']) && !empty($query['time_e'])) {
            $cond .=" and add_time<='{$query['time_e']}'";
        }
        if (isset($query['time_s2']) && !empty($query['time_s2'])) {
            $cond .=" and up_time>='{$query['time_s2']}'";
        }
        if (isset($query['time_e2']) && !empty($query['time_e2'])) {
            $cond .=" and up_time<='{$query['time_e2']}'";
        }
        if (isset($query['order_status']) && !empty($query['order_status'])) {
            $cond .=" and order_status='{$query['order_status']}'";
        }

        return $cond;
    }

    
}